<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\model\Position\PositionBaseInfo;
use Doraemon\pockets\IdSender\IdSender;
use Doraemon\service\Article;
use Doraemon\service\statSign\Basic;
use Doraemon\tools\BlockType;
use Doraemon\tools\ImgUrl;
use Doraemon\tools\LinkFormat;
use Doraemon\tools\StatSign;

class HouseCard extends BaseModule
{

    const FORMAT_HIT = 'formatPosHit';

    /**
     * 推荐线api格式化输出
     * @param $hits
     * @param $uid
     * @param $userProfileExt
     * @param $statSignExt
     * @return array
     */
    public function format($hits, $uid, $userProfileExt, $statSignExt)
    {
        $response = [];
        if ($hits) {
            foreach ($hits as $hit) {
                //公共信息赋值
                $index = intval($hit['pos']);
                $adm_comment = (string)$hit['remark'];
                $reason = $hit['reason'];
                $type = null;
                $map = [
                    PositionBaseInfo::REASON_TYPE_BUSINESS => 'biz_ad',
                    PositionBaseInfo::REASON_TYPE_KA => 'disigner_ad',
                    PositionBaseInfo::REASON_TYPE_OPERATE => 'op_suggest',
                    PositionBaseInfo::REASON_TYPE_NEW_USER => 'welcome_new_user',
                ];
                $params = [
                    'suggestsign' => $map[$reason] ?? '',
                    'pos_id' => $hit['id'],
                    'page' => 0,
                ];
                if (in_array($hit['ext_type'], [self::EXT_TYPE_KA])) {
                    //KA广告
                    $ka = json_decode($hit['pic_id'], true);
                    $obj_id = $ka['obj_id'] ?? '';
                    $type = $ka['obj_type'] ?? null;
                    $posi = StatSign::ARTICLE_KA;

                    if (!$obj_id) {
                        continue;
                    }

                    $type = isset($type) ? $type : IdSender::getType($obj_id);
                    $tempData = [];
                    switch ($type) {
                        case IdSender::TYPE_ARTICLE:
                            $type = BlockType::TYPE_ARTICLE;
                            $article_list = Article::getArticlesForDynamic([$obj_id], $uid);
                            if ($article_list && isset($article_list[$obj_id])) {
                                $tempData = [
                                    'article' => $article_list[$obj_id],
                                ];
                            }
                            break;
                        default:
                            break;
                    }

                    if ($tempData) {
                        $tempData = [
                                'index' => $index,
                                'type' => $type,
                                'obj_id' => $obj_id,
//                                'statSign' => StatSign::getStatSignForAd($posi, $obj_id, $idx = 0, $index, $adm_comment, ['params' => $params])
                                'statSign' => $this->statSign($hit, [
                                    Basic::EXTEND_KEY_CONFIG_ID => $hit['id'],
                                    Basic::EXTEND_KEY_POS => $index,
                                    Basic::EXTEND_KEY_OBJ_ID => $obj_id,
                                    Basic::EXTEND_KEY_REASON_TYPE => $map[$reason] ?? '',
                                ]),
                            ] + $tempData;
                    }
                } else {
                    $pic_ids = json_decode($hit['pic_id'], true);
                    $pic_id = $pic_ids['single_pic_id'] ?? '';
                    $link = LinkFormat::generateLink($hit['link_type'], $hit['link_params']);
                    $link = $link['app_template'];
                    $banner_list = [
                        'id' => $hit['id'],
                        'index' => $index,
                        'banner' => empty($pic_id) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_id, ImgUrl::IMAGE_SIZE_OP_ORI),
                        'double_banner' => empty($pic_id) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_id, ImgUrl::IMAGE_SIZE_OP_ORI),
                        'link' => $link,
                        'statType' => (string)'banner' . BlockType::TYPE_CAROUSEL_BANNER . '-10',
//                        'statSign' => StatSign::getStatSignForAd(StatSign::ARTICLE_BANNER, $hit['id'], 0, $index, $adm_comment, ['params' => $params]),
                        'statSign' => $this->statSign($hit, [
                            Basic::EXTEND_KEY_CONFIG_ID => $hit['id'],
                            Basic::EXTEND_KEY_POS => $index,
                            Basic::EXTEND_KEY_REASON_TYPE => $map[$reason] ?? '',
                        ]),
                    ];
                    $tempData = [
                        'type' => BlockType::TYPE_CAROUSEL_BANNER,
                        'banner_list' => [$banner_list],
                        'banner' => $banner_list,
                        'index' => $index,
                    ];
                }

                if ($tempData) {
                    $response[$hit['ext_type']][$index] = $tempData;
                }
            }
        }
        return $response;
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        $pic_ids = json_decode($v['pic_id'], true);
        $ext_type = $v['ext_type'];
        switch ($ext_type) {
            case self::EXT_TYPE_FLOW:
                $pic_id = $pic_ids['single_pic_id'] ?? '';
                $v['pic_url'] = empty($pic_id) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_id, ImgUrl::IMAGE_SIZE_OP_ORI);
                break;
            case self::EXT_TYPE_KA:
                $v['obj_type'] = $pic_ids['obj_type'] ?? '';
                $v['obj_id'] = $pic_ids['obj_id'] ?? '';
                break;
            default:
                $v['single_pic_url'] = empty($pic_ids['single_pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_ids['single_pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
                $v['double_pic_url'] = empty($pic_ids['double_pic_id']) ? '' : ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $pic_ids['double_pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
                break;
        }
    }

    public function uniquePos($v): string
    {
        return $v['pos'];
    }

    public function getExtInfo($baseInfo)
    {
        $ext_type = $baseInfo['ext_type'] ?? self::EXT_TYPE_FLOW;
        $link_type = $baseInfo['link_type'] ?? '';
        $link_params = $baseInfo['link_params'] ?? '';
        $pic_id = json_decode($baseInfo['pic_id'], true);
        $response = [
            'ext_type' => $ext_type
        ];
        switch ($ext_type) {
            case self::EXT_TYPE_FLOW:
                $response['single_pic_id'] = $pic_id['single_pic_id'] ?? '';
                $response['double_pic_id'] = $pic_id['double_pic_id'] ?? '';
                break;
            case self::EXT_TYPE_KA:
                $response['obj'] = compact('link_type', 'link_params');
                break;
            default:
                break;
        }

        return $response;
    }

}